package com.cskaoyan.service.search;

import com.cskaoyan.bean.MallKeyword;
import com.cskaoyan.bean.MallKeywordExample;
import com.cskaoyan.bean.MallSearchHistory;
import com.cskaoyan.bean.MallSearchHistoryExample;
import com.cskaoyan.mapper.MallKeywordMapper;
import com.cskaoyan.mapper.MallSearchHistoryMapper;
import com.cskaoyan.model.vo.wxsearch.SearchHistoryData;
import com.cskaoyan.model.vo.wxsearch.SearchIndexData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: zhijie
 * @create: 2021-12-02 15:43
 **/
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    MallSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MallKeywordMapper mallKeywordMapper;

    /**
     * 查询热门关键词及默认关键词
     * 判断id是否为Null,为Null 则未登录,不进行用户的查询
     *
     * @param id
     * @return
     */
    @Override
    public SearchIndexData searchIndex(Integer id) {
        MallKeywordExample example = new MallKeywordExample();

        MallKeywordExample.Criteria criteria = example.createCriteria().andIsDefaultEqualTo(true);
        criteria.andDeletedEqualTo(false);

        List<MallKeyword> mallKeywords = mallKeywordMapper.selectByExample(example);

        example.clear();
        MallKeywordExample.Criteria criteria1 = example.createCriteria().andIsHotEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        example.setOrderByClause("sort_order asc");
        List<MallKeyword> mallKeywords1 = mallKeywordMapper.selectByExample(example);
        SearchIndexData data = new SearchIndexData();
        data.setHotKeywordList(mallKeywords1);
        data.setDefaultKeyword(mallKeywords.get(0));
        //判断id是否为null,判断用户是否登录
        if (id != null) {
            List<SearchHistoryData> historyDataList = searchHistoryMapper.selectKeywordById(id);
            data.setHistoryKeywordList(historyDataList);
        }
        return data;
    }

    @Override
    public List<String> searchHelper(String keyword) {
        List<String> data = mallKeywordMapper.selectByKeyword(keyword);
        return data;
    }

    @Override
    public void searchClearhistory(Integer id) {
        MallSearchHistory mallSearchHistory = new MallSearchHistory();
        mallSearchHistory.setUpdateTime(new Date());
        mallSearchHistory.setDeleted(true);
        mallSearchHistory.setUserId(id);
        searchHistoryMapper.updateDeletedById(mallSearchHistory);


    }
}
